﻿@{
    ViewBag.Title = "Iterator";
    Layout = "~/Views/Shared/_Page.cshtml";
}

<h2>Iterator</h2>

<script type="text/javascript">
    var agg = (function () {
        var index = 0,
            data = [1, 2, 3, 4, 5],
            length = data.length;

        return {
            next: function () {
                var element;
                if (!this.hasNext()) {
                    return null;
                }
                element = data[index];
                index++;

                return element;
            },

            hasNext: function () {
                return index < length;
            },

            rewind: function () {
                index = 0;
            },

            current: function () {
                return data[index];
            }
        };
    }());

    window.onload = function () {
        while (agg.hasNext()) {
            ZEUS.DEBUG.log(agg.next());
        }

        // go back
        ZEUS.DEBUG.log('Go back to top...');
        agg.rewind();
        ZEUS.DEBUG.log(agg.current()); // 1
    }
</script>

